#include <windows.h>
#include "handle.h"

#define PIXELWIDE         10
#define WINDOW_SIZE_UP    450
#define WINDOW_SIZE_WIDE  515

LRESULT CALLBACK WndProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
void paint_field ( HDC hdc, HWND hwnd );
void pain_element ( HDC hdc, int x, int y, COLORREF color );
char Snake_field [SNAKE_HORIZONTAL] [SNAKE_VERTICAL] = {0};
SNAKE_handle user_snake ( Snake_field );


int WINAPI WinMain( HINSTANCE hI, HINSTANCE hPrI, PSTR szCmdLine, int iCmdShow )
{    
	static TCHAR szName[] = TEXT("SNAKE_TEST");    
	HWND hwnd;

	WNDCLASS wc;
	wc.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
	wc.lpfnWndProc   = WndProc ;    
	wc.cbClsExtra    = 0 ;
	wc.cbWndExtra    = 0 ;    
	wc.hInstance     = hI;
	wc.hIcon         = LoadIcon( NULL, IDI_WINLOGO );
	wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = (HBRUSH) GetStockObject( WHITE_BRUSH );
	wc.lpszMenuName  = NULL ;    
	wc.lpszClassName = szName ;
	RegisterClass( &wc );

	hwnd = CreateWindow( szName, szName, WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 515, 450, NULL, NULL, hI, NULL );       
	 
	ShowWindow   ( hwnd, iCmdShow );    
	UpdateWindow ( hwnd );    
	

	
	MSG msg;    
	while( GetMessage( &msg, NULL, 0, 0 ) )    
	{
		TranslateMessage(&msg);         
		DispatchMessage (&msg);
	}
	return msg.wParam ;
}

LRESULT CALLBACK WndProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
  static bool first_step = true;
 	static bool step_in_work = false;
  HDC hdc; 
  PAINTSTRUCT ps;
  if ( first_step )
  {
  	user_snake.set_snake ( Snake_field );
	  user_snake.last_direction = SNAKE_RIGHT;
	  first_step = false;
  }
  switch ( message )
  {
    case WM_CLOSE:
    {
      PostQuitMessage (0);
      return 0;
    }
    case WM_PAINT:
    {
      hdc = BeginPaint ( hwnd, &ps );
      if ( user_snake.self_eat ( Snake_field ) )
      {
        paint_field ( hdc, hwnd );
      }
      else 
      {
        MessageBox ( hwnd, L"Game Over!!!", L"You have failed!", NULL );
        PostQuitMessage ( 0 );
      }
      EndPaint ( hwnd, &ps );
      step_in_work = false;
      InvalidateRect ( hwnd, NULL, true );
      Sleep ( 100 );
      return 0;
    }
       case WM_CHAR:
    {
      if ( !step_in_work )
      {
        step_in_work = true;
        switch ( wParam ) 
        {
          case 'w':
          case 'W':
          {
            if ( user_snake.last_direction != SNAKE_DOWN  )
              user_snake.last_direction = SNAKE_HIGH;
            break;
          }
          case 'a':
          case 'A':
          {
            if ( user_snake.last_direction != SNAKE_RIGHT )
              user_snake.last_direction = SNAKE_LEFT;
            break;
          }
          case 's':
          case 'S':
          {
            if ( user_snake.last_direction != SNAKE_HIGH ) 
              user_snake.last_direction = SNAKE_DOWN;
            break;
          }
          case 'd':
          case 'D':
          {
            if ( user_snake.last_direction != SNAKE_LEFT )
              user_snake.last_direction = SNAKE_RIGHT;
            break;
          }
        }
        return 0;
      }
    }
  }
  return DefWindowProc ( hwnd, message, wParam, lParam );
}

void pain_element ( HDC hdc, int x, int y, COLORREF color )
{
  int i = 0;
  int j = 0;
  for ( i = 0; i < PIXELWIDE; i++ )
  {
    for ( j = 0; j < PIXELWIDE; j++ )
    {
      SetPixel ( hdc, ( y * PIXELWIDE ) + PIXELWIDE + i, ( x * 10 ) + 5 + j, color );
    }
  }
}

void paint_field ( HDC hdc, HWND hwnd )
{
  bool food_have_to_set = user_snake.snake_move ( Snake_field );
  for ( int i = 0; i < SNAKE_HORIZONTAL; i++ )
  {
    for ( int j = 0; j < SNAKE_VERTICAL; j++ )
    { 
      if ( user_snake.is_Snake ( i, j, Snake_field ) == SNAKE_BODY )
      {
        pain_element ( hdc , i, j, RGB ( 0, 255, 0 ) );
      }
      else if ( user_snake.is_Snake ( i, j, Snake_field ) == SNAKE_FOOD )
      {
        pain_element ( hdc , i, j, RGB ( 255, 0, 0 ) );
      }
    }
  }
}